package com.lmuse.xmr.foodspotting.db;

import com.lmuse.xmr.foodspotting.model.Recipe;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class DiscoverPresenterImpl implements Contract.DiscoverPresenter {

    private Contract.DiscoverView discoverView;

    public DiscoverPresenterImpl(Contract.DiscoverView discoverView) {
        this.discoverView = discoverView;
    }

    @Override
    public void queryDiscoverRecipe(List<String> objectIdList) {
        List<BmobQuery<Recipe>> queries = new ArrayList<BmobQuery<Recipe>>();

        BmobQuery<Recipe> eq1 = new BmobQuery<Recipe>();
        eq1.addWhereEqualTo("author", "2qLBRRRz");  //管理员（默认关注）
        queries.add(eq1);

        for (String objectId : objectIdList) { //关注的其他人
            BmobQuery<Recipe> eq = new BmobQuery<Recipe>();
            eq.addWhereEqualTo("author", objectId);
            queries.add(eq);
        }

        BmobQuery<Recipe> mainQuery = new BmobQuery<Recipe>();
        mainQuery.or(queries);
        mainQuery.findObjects(new FindListener<Recipe>() {
            @Override
            public void done(List<Recipe> object, BmobException e) {
                if(e==null){
                    discoverView.onQueryDiscoverRecipeSuccess(object);
                }else{
                    discoverView.onQueryDiscoverRecipeFailed(e.toString());
                }
            }
        });
    }

}
